<?php
	class ConnDB{
		private  $conn;
		public  function __construct($host,$user,$pwd,$dbname,$charset){
			$this->conn=@mysql_connect($host,$user,$pwd)
				or die ('数据库连接失败:'.mysql_error());
			@mysql_select_db($dbname) or die('数据库选择失败'.mysql_error());
			@mysql_set_charset($charset);
		}

		//查询结果集
		 function query($sql){
			// 截取字符
			$sqltype=strtolower(substr(trim($sql), 0,6));
			$result=mysql_query($sql,$this->conn) or die('查询失败: '.mysql_error());
			$callback_array=array();
			if("select"==$sqltype){
				if($result==false){
					return false;
				}
				else if(0==mysql_num_rows($result)){
					return false;
				}
				
				else {
					while($result_array=mysql_fetch_array($result)){
						array_push($callback_array,$result_array);
					}
					return $callback_array;
				}
			}
			else if('update'==$sqltype ||'insert'==$sqltype ||'delete'==$sqltype){
				return $result;
			}

		}

		function __destruct(){
			mysql_close($this->conn);
		}

		function update($tbl,$data,$where=null){
			    $sets='';
			
			    foreach ($data as $key=>$val){
			        $sets.=$key."='".$val."',";
			       	// $set  = $set  .     $key. "'=".$val."',";
					//	example:
					//	$stu = array('stuno'=>1111,'sex'=>1);
					//	第1次遍历
					//	$set = "stuno='1111',";
					//	第2次遍历
					//	 $set = $set ."sex='1',";
					//	$set = "stuno = '1111',sex='1',";
			    }
			    $sets=rtrim($sets,','); //去掉SQL里的最后一个逗号
			    $where=$where==null?'':' WHERE '.$where;
			    $sql="UPDATE {$tbl} SET {$sets} {$where}";
			   
			    $res=mysql_query($sql);
			    if ($res){
			        return mysql_affected_rows();
			    }else {
			        return false;
			    }
}
	}
?>